package com.mediacloud.app.newsmodule.activity.audio.datepicker

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.ColorInt
import com.mediacloud.app.newsmodule.R;

/**
 * Powered by jzman.
 * Created on 2019/1/3 0003.
 */
@SuppressLint("InflateParams")
class DatePickerDialog private constructor(context: Context) :
    Dialog(context, R.style.DatePicker_Dialog) {
    private var mDateType: DateType? = null
    private var mYearRange: Pair<Int, Int>? = null
    private var mTitle: String? = null
    private var mHighlightColor: Int? = null
    private var isCanceledTouchOutside: Boolean? = null
    private var mOnDateResult: ((date: Long) -> Unit)? = null
    private var datePickerView: DatePickerView? = null
    private var tvDialogTitle: TextView? = null
    private var tvDialogTopConfirm: TextView? = null
    private var tvDialogTopCancel: TextView? = null
    private var listener: ClickListener? = null

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.wcy_dialog_date_picker, null, false)
        tvDialogTitle = view.findViewById(R.id.tvDialogTitle)
        tvDialogTopConfirm = view.findViewById(R.id.tvDialogTopConfirm)
        tvDialogTopCancel = view.findViewById(R.id.tvDialogTopCancel)
        setContentView(view)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setWindowParams()
        init()
    }

    private fun setWindowParams() {
        val params = window?.attributes
        params?.gravity = Gravity.BOTTOM
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        window?.attributes = params
    }

    private fun init() {
        datePickerView?.setDateType(mDateType)
        datePickerView?.setYearRange(mYearRange)
        tvDialogTitle?.text = mTitle ?: "日期选择"
        val highlightColor = mHighlightColor ?: Color.BLACK
        datePickerView?.setHighlightColor(highlightColor)
        tvDialogTopConfirm?.setTextColor(highlightColor)
        setCanceledOnTouchOutside(isCanceledTouchOutside ?: true)

        tvDialogTopCancel?.setOnClickListener {
            dismiss()
        }
        /*tvDialogTopConfirm?.setOnClickListener {
            dismiss()
            datePickerView?.getSelectedDate()?.let { date -> mOnDateResult?.invoke(date) }
        }*/
        tvDialogTopConfirm?.setOnClickListener {
//            datePickerView?.getSelectedDate2()?.let {
//                listener?.onClick(it)
//            }
            val bean = TimeBean()
            bean.year = datePickerView?.mCurrentYear.toString()
            bean.month = datePickerView?.mCurrentMonth.toString()
            listener?.onClick(bean)
//            datePickerView?.getSelectedDate()?.let { date -> mOnDateResult?.invoke(date) }

            dismiss()
        }
    }

    interface ClickListener {
        fun onClick(date: TimeBean)
    }

    class Builder(private val context: Context) {
        private var dateType: DateType? = null
        private var yearRange: Pair<Int, Int>? = null
        private var title: String? = null
        private var highlightColor: Int? = null
        private var isCanceledTouchOutside: Boolean? = null
        private var onDateResult: ((date: Long) -> Unit)? = null
        private var listener: ClickListener? = null

        fun setDateType(dateType: DateType): Builder {
            this.dateType = dateType
            return this
        }

        fun setListener(listener: ClickListener): Builder {
            this.listener = listener
            return this
        }

        fun setYearRange(start: Int, end: Int): Builder {
            if (start > end) {
                throw IllegalArgumentException("开始年份不能大于结束年份")
            }
            this.yearRange = Pair(start, end)
            return this
        }

        fun setTitle(title: String?): Builder {
            this.title = title
            return this
        }

        fun setHighlightColor(@ColorInt color: Int): Builder {
            this.highlightColor = color
            return this
        }

        fun setCanceledTouchOutside(canceledTouchOutside: Boolean): Builder {
            this.isCanceledTouchOutside = canceledTouchOutside
            return this
        }

        fun setOnDateResult(onDateResult: ((date: Long) -> Unit)): Builder {
            this.onDateResult = onDateResult
            return this
        }

        fun build(): DatePickerDialog {
            val dialog = DatePickerDialog(context)
            dialog.mDateType = dateType
            dialog.mYearRange = yearRange
            dialog.mTitle = title
            dialog.listener = listener
            dialog.mHighlightColor = highlightColor
            dialog.isCanceledTouchOutside = isCanceledTouchOutside
            dialog.mOnDateResult = onDateResult
            return dialog
        }
    }
}